<?php
/**
 * Created by PhpStorm.
 * User: luzy
 * Date: 2018/6/27
 * Time: 上午11:11
 */

class Passport extends Base_Controller
{
    public function __construct()
    {
        parent::__construct();
        $this->load->model('mp_model');
    }

    public function login()
    {
        $code=$this->input->get('code');
        $mpid=$this->input->get('mpid');
        if (empty($code)||empty($mpid)) $this->error();

        $this->mp_model->setAppParams($mpid);
        $sessiondata = $this->mp_model->jscode2session($code);
        $uid=$this->mp_model->getUid($sessiondata,$mpid);
        if ($uid!==false){
            $this->success(array('uid'=>$uid));
        }else{$this->error(102);}
    }

    public function addUserInfo()
    {
        $rawdata = $this->input->raw_input_stream;
        $data = json_decode($rawdata,true);
        if (empty($data['uid'])||empty($data['userinfo'])) $this->error();
        $res = $this->mp_model->addUserInfo($data['uid'],json_decode($data['userinfo'],true));
        $this->success($res);
    }

    public function test()
    {
        if (!is_cli()) die('request forbidden');
        $dir = '/home/nldou/webroot/web/upload/images/act/dayquot/mon/4';
        $sourcedir = $dir.'/source';
        $cropdir = $dir.'/crop';

        $dir_handle = opendir($sourcedir);
        while (false !== ($file=readdir($dir_handle))){
            if ($file=='.'||$file=='..'){
                continue;
            }
            echo $file;
            $parts = explode('.jpg',$file);
            $filename = $parts[0];
            $ext = '.jpg';
            $newname = $filename.'.'.time();
            //处理图片
            $this->load->model('post_model');
//            $config=array();
//            $config['source_image'] = $sourcedir.'/'.$file;
//            $config['new_image'] = $cropdir.'/'.$newname.$ext;
//            $config['width'] = '668';
//            $this->post_model->resize($config);

            $config['maintain_ratio'] = false;
            $config['source_image'] = $cropdir.'/'.$newname.$ext;
            $config['new_image'] = $cropdir.'/'.$newname.$ext;
            $config['width'] = '668';
            $config['height'] = '668';
            $config['x_axis']='0';//1:41,2:26,3:26,4:0
            $config['y_axis']='205';//1:450,2:250,3:400,4:205
            $this->post_model->crop($config);
            echo ' success'.PHP_EOL;
        }

//        $config['source_image'] = $new;
//        $config['width'] = '668';
//        $config['height'] = '1002';
//        $config['x_axis'] = '-41';
//        $config['y_axis'] = '-262';
//        $this->post_model->crop($config);
//        echo $filename.' success';
    }
}